package com.hongying.lingtianxiang;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class PaintDemo {
    private Graphics2D g = null;

    /**
     * 导入本地图片到缓冲区
     */
    public BufferedImage loadImageLocal(String imgName) {
        try {
            return ImageIO.read(new File(imgName));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    public BufferedImage modifyImagetogeter(BufferedImage b, BufferedImage d) {

        try {
            int w = b.getWidth();
            int h = b.getHeight();
            System.out.println("w: " + w);
            System.out.println("h: " + h);

            g = d.createGraphics();
            g.drawImage(b, 0, 0, 100, 100, null);
            g.drawString("张三",500,30);
            g.dispose();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return d;
    }

    /**
     * 生成新图片到本地
     */
    public void writeImageLocal(String newImage, BufferedImage img) {
        if (newImage != null && img != null) {
            try {
                File outputfile = new File(newImage);
                ImageIO.write(img, "jpg", outputfile);
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }

    public static void main(String[] args) throws IOException {

        PaintDemo tt = new PaintDemo();

        BufferedImage d = tt.loadImageLocal("C:\\Users\\qwer\\Desktop\\a.png");
        BufferedImage b = tt.loadImageLocal("C:\\Users\\qwer\\Desktop\\b.jpg");

        tt.writeImageLocal("C:\\Users\\qwer\\Desktop\\d.png", tt.modifyImagetogeter(d, b));
        //将多张图片合在一起
        System.out.println("success");

    }
}
